package com.admin.business.venusProjectLifeServiceCost;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import com.zmbsms.os.core.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description: 生活服务订单信息表
 * @create: 2023-11-07 10:53:04
 **/
@Service("venusProjectLifeServiceCostService")
@Slf4j
public class VenusProjectLifeServiceCostService {

    @Resource
    VenusProjectLifeServiceCostDao venusProjectLifeServiceCostDao;

    /**
     * 添加生活服务订单信息表,获取order_id数据
     *
     * @param cdoRequest - order_id:主键
     *                   - code:订单号
     *                   - business_id:商家id
     *                   - p_id:服务id
     *                   - pro_id:小区id
     *                   - user_id:缴费人id
     *                   - cost_type:订单类型：1.商户认证 2.服务缴费
     *                   - cost_state:订单状态：0.已下单1.已接单2.待评价3.待退款4.已完成
     *                   - refund_state:退款状态：0.退款中1.退款完成2.退款失败
     *                   - cost_money:交易金额
     *                   - cost_remark:备注
     *                   - begin_time:时间
     *                   - state:状态
     *                   - pay_type:支付方式0.微信1.支付宝
     * @return
     * @author liuzir
     * @create: 2023-11-07 10:53:04
     */
    @OpenApi
    public Return addVenusProjectLifeServiceCost(CDO cdoRequest) {
        venusProjectLifeServiceCostDao.addVenusProjectLifeServiceCost(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("order_id")) {
            cdoResult.set("order_id", cdoRequest.get("order_id"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据order_id，更新生活服务订单信息表数据
     *
     * @param cdoRequest - order_id:主键
     *                   - code:订单号
     *                   - business_id:商家id
     *                   - p_id:服务id
     *                   - pro_id:小区id
     *                   - user_id:缴费人id
     *                   - cost_type:订单类型：1.商户认证 2.服务缴费
     *                   - cost_state:订单状态：0.已下单1.已接单2.待评价3.待退款4.已完成
     *                   - refund_state:退款状态：0.退款中1.退款完成2.退款失败
     *                   - cost_money:交易金额
     *                   - cost_remark:备注
     *                   - begin_time:时间
     *                   - state:状态
     *                   - pay_type:支付方式0.微信1.支付宝
     * @return
     * @author liuzir
     * @create: 2023-11-07 10:53:04
     */
    @OpenApi
    public Return updateVenusProjectLifeServiceCostById(CDO cdoRequest) {
        venusProjectLifeServiceCostDao.updateVenusProjectLifeServiceCostById(cdoRequest);
        return Return.SUCCESS;
    }

    /**
     * 根据order_id，删除生活服务订单信息表数据
     *
     * @param cdoRequest - order_id:主键
     * @return
     * @author liuzir
     * @create: 2023-11-07 10:53:04
     */
    @OpenApi
    public Return deleteVenusProjectLifeServiceCostById(CDO cdoRequest) {
        int count = venusProjectLifeServiceCostDao.deleteVenusProjectLifeServiceCostById(cdoRequest.getLong("order_id"));
        CDO cdoResult = new CDO();
        cdoResult.set("count", count);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除生活服务订单信息表数据
     *
     * @param cdoRequest
     * @return
     * @author liuzir
     * @create: 2023-11-07 10:53:04
     */
    @OpenApi
    public Return deleteVenusProjectLifeServiceCost(CDO cdoRequest) {
        int count = venusProjectLifeServiceCostDao.deleteVenusProjectLifeServiceCost(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("count", count);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取生活服务订单信息表数据
     *
     * @param cdoRequest - order_id:主键
     * @return
     * @author liuzir
     * @create: 2023-11-07 10:53:04
     */
    @OpenApi
    public Return getVenusProjectLifeServiceCostById(CDO cdoRequest) {
        if (StringUtils.isEmpty(cdoRequest.getString("code"))){
            return Return.valueOf(-1,"订单号不可为空！");
        }
        CDO serviceCost = venusProjectLifeServiceCostDao.getVenusProjectLifeServiceCostById(cdoRequest);
        return Return.valueOf(200,serviceCost);
    }

    /**
     * 订单列表分页
     *
     * @param cdoRequest
     * @return
     * @author liuzir
     * @create: 2023-11-07 10:53:04
     */
    @OpenApi
    public Return getVenusProjectLifeServiceCostListPage(CDO cdoRequest) {
        //分页参数
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = venusProjectLifeServiceCostDao.getVenusProjectLifeServiceCostListPage(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
        int total = venusProjectLifeServiceCostDao.getVenusProjectLifeServiceCostCount(cdoRequest);
        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 商户订单日，月收益统计
     */
    @OpenApi
    public Return getCommercialOrderMonthStatistics(CDO cdoRequest){
        if (StringUtils.isEmpty(cdoRequest.getString("commercial_id"))) {
            return Return.valueOf(-1, "商家id不可为空！");
        }
        if (StringUtils.isEmpty(cdoRequest.getString("year"))) {
            return Return.valueOf(-1, "年份不可为空！");
        }
        if (StringUtils.isEmpty(cdoRequest.getString("month"))) {
            return Return.valueOf(-1, "月份不可为空！");
        }
        CDO orderStatistics = venusProjectLifeServiceCostDao.selectCommercialOrderMonthStatistics(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("orderStatistics",orderStatistics);
        return Return.valueOf(200,cdoResult);
    }

    /**
     * 商户订单年收益，总收益统计
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return getCommercialOrderYearStatistics(CDO cdoRequest){
        if (StringUtils.isEmpty(cdoRequest.getString("commercial_id"))) {
            return Return.valueOf(-1, "商家id不可为空！");
        }
        List<CDO> orderStatisticsList = venusProjectLifeServiceCostDao.selectCommercialOrderYearStatistics(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("orderStatisticsList",orderStatisticsList);
        return Return.valueOf(200,cdoResult);
    }

    /**
     * 商户订单量统计
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return getCommercialOrderNumStatistics(CDO cdoRequest){
        if (StringUtils.isEmpty(cdoRequest.getString("business_id"))) {
            return Return.valueOf(-1, "商家id不可为空！");
        }
        List<CDO> orderNumStatistics = venusProjectLifeServiceCostDao.selectCommercialOrderNumStatistics(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("orderNumStatistics",orderNumStatistics);
        return Return.valueOf(200,cdoResult);
    }

    /**
     * 通过商家id和时间获取服务订单详情
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return getServicesByCommercialIdAndTime(CDO cdoRequest){
        if (StringUtils.isEmpty(cdoRequest.getString("business_id"))) {
            return Return.valueOf(-1, "商家id不可为空！");
        }
        if (StringUtils.isEmpty(cdoRequest.getString("create_time"))) {
            return Return.valueOf(-1, "创建时间不可为空！");
        }
        List<CDO> serviceList = venusProjectLifeServiceCostDao.selectServiceListByCommId(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("serviceList",serviceList);
        return Return.valueOf(200,cdoResult);
    }

    /**
     * 更新商户订单状态
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return commercialOrderReceiving(CDO cdoRequest){
        try {
            if(StringUtils.isEmpty(cdoRequest.getString("code"))){
                return Return.valueOf(-1,"订单号不可为空！");
            }
            venusProjectLifeServiceCostDao.updateVenusProjectLifeServiceCostById(cdoRequest);
            return Return.valueOf(200,"修改订单状态成功！");
        }catch (Exception e){
            return Return.valueOf(-1,"修改订单状态失败！");
        }
    }

    /**
     * 获取所有订单状态
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return getOrderStateList(CDO cdoRequest){
        List<CDO> allOrderList = venusProjectLifeServiceCostDao.selectAllOrderState();
        CDO cdoResult = new CDO();
        cdoResult.set("allOrderList",allOrderList);
        return Return.valueOf(200,cdoResult);
    }
}
